#include "multi_tree.h"

MTree::MTree(FileInfo Data) :file(Data), father(nullptr), child(nullptr), brother(nullptr), lastchild(nullptr) {};
MTree::~MTree() {};

QSharedPointer<MTree> MTree::addChild(FileInfo newChild)
{
    QSharedPointer<MTree> This = sharedFromThis();
    if (!This->child)
    {
        This->child = QSharedPointer<MTree>::create(newChild);
        This->child->father = This;
        This->lastchild = This->child;
    }
    else
    {
        This->lastchild->brother = QSharedPointer<MTree>::create(newChild);
        This->lastchild = This->lastchild->brother;
        This->lastchild->father = This;
    }
    return This->lastchild;
}

void MTree::Accumulate()
{
    QSharedPointer<MTree> currentNode = sharedFromThis()->child;
    while (currentNode != nullptr)
    {
        currentNode->Accumulate();
        this->file.SizePlus(currentNode->file.getSize());
        this->file.DirPlus(currentNode->file.getDirNum());
        this->file.FilePlus(currentNode->file.getFileNum());
        currentNode = currentNode->brother;
    }
}

FileInfo MTree::getFile() { return this->file; }
QSharedPointer<MTree> MTree::getFather() { return this->father; }
QSharedPointer<MTree> MTree::getChild() { return this->child; }
QSharedPointer<MTree> MTree::getBrother() { return this->brother; }
QSharedPointer<MTree> MTree::getLastChild() {return this->lastchild;}

int MTree::getNumOfChildFile()
{
    int cnt=0;
    QSharedPointer<MTree> ptr=this->child;
    while(ptr)
    {
        if(ptr->getFile().getType()!="Dir") cnt++;
        ptr=ptr->brother;
    }
    return cnt;
}

QStringList GetInfoList(QSharedPointer<MTree> Node)
{
    FileInfo file=Node->getFile();
    qint64 currentSize=file.getSize();
    qint64 fatherSize=Node->getFather()?Node->getFather()->getFile().getSize():currentSize;
    double rate=100.0*currentSize/fatherSize;
    QString Rate=QString::number(rate,'f',3);
    QString Size=showFollowedUnit(file.getSize());
    QString numOfFile=QString::number(file.getFileNum());
    QString numOfDir=QString::number(file.getDirNum());
    QStringList InfoList;
    InfoList<<file.getName()<<Rate<<Size<<numOfFile<<numOfDir<<file.getWriteTime().toString("yyyy-MM-dd HH:mm:ss")<<file.getCreateTime().toString("yyyy-MM-dd HH:mm:ss")<<file.getAccessTime().toString("yyyy-MM-dd HH:mm:ss");
    return InfoList;
}
